<?php
$className = "Group";
@include_once "installableInterface.inc";
/* 
 * All operations concerning a single Group
 * are to be handled in the Group class.
 */
class Group implements Installable {
    private $id = "";
    private $name = "";
    private $gRead = "";
    private $gInsert = "";
    private $gUpdate = "";
    private $gCreate = "";
    private $gDelete = "";
    private $db = null;

    function  __construct() {
        $argv = func_get_args();
        switch( func_num_args() )
        {
            case 1:
                self::defConstructor($argv[0]);
                break;
            case 2:
                list($db,$id) = $argv;
                idConstructor($db,$id);
                break;
            case 7:
                list($db, $name, $gRead, $gInsert, $gUpdate, $gCreate, $gDelete) = $argv;
                self::fullConstructor($db, $name, $gRead, $gInsert, $gUpdate, $gCreate, $gDelete);
                break;
            case 8:
                list($db, $name, $gRead, $gInsert, $gUpdate, $gCreate, $gDelete, $id) = $argv;
                self::fullConstructor($db, $name, $gRead, $gInsert, $gUpdate, $gCreate, $gDelete, $id);
                break;
        }
    }
    /**
     * The constructor expectes at least a Database object
     * @param Database $db
     */
    private function defConstructor($db) {
        $this->db = $db;
    }
    /**
     * The constructor will initialize the object by the data in the
     * database given the provided id
     * @param Database $db
     * @param String $id
     */
    private function idConstructor($db, $id) {
        $this->db = $db;
        $this->setGroup($id);
    }
    /**
     * The object is constructed given all the necessary fields
     * where id is optional and doesn't have to be provided.
     * @param Database $db
     * @param String $id
     * @param String $name
     * @param String $gRead
     * @param String $gInsert
     * @param String $gUpdate
     * @param String $gCreate
     * @param String $gDelete
     */
    private function fullConstructor($db, $name, $gRead, $gInsert, $gUpdate, $gCreate, $gDelete, $id="-1") {
        $this->db = $db;
        $this->id = $id;
        $this->name = $name;
        $this->gRead = $gRead;
        $this->gInsert = $gInsert;
        $this->gUpdate = $gUpdate;
        $this->gCreate = $gCreate;
        $this->gDelete = $gDelete;
    }
    /**
     * This private function supports retrieval of the data concerning
     * a group given an id.
     * @param String $id
     * @return boolean false if the group was not found (i.e. id doesn't exist).
     */
    private function setGroup($id) {
        Security::safeToUse($id);
        $sql = "
            SELECT * 
            FROM Groups 
            WHERE ID = $id";
        $res = $this->db->getResult($sql);
        if(!$res)
            return(false);
        if(count($res) <= 0)
            return(false);
        $this->id = $res[0]['ID'];
        $this->name = $res[0]['name'];
        $this->gRead = $res[0]['gRead'];
        $this->gInsert = $res[0]['gInsert'];
        $this->gUpdate = $res[0]['gUpdate'];
        $this->gCreate = $res[0]['gCreate'];
        $this->gDelete = $res[0]['gDelete'];
        return(true);
    }
    /**
     * Returns a Group object given the database id.
     * @param String $id
     * @return Group
     */
    public function getGroup($id) {
        $this->setGroup($id);
        return($this);
    }

    /**
     * If the table Groups exists it can be dropped in case
     * a reinstallation is neccessary for instance.
     * @param <link identifier> $db is the database handle.
     */
    public function dropTable() {
        $sql = "DROP TABLE IF EXISTS Groups";
        $rs = $this->db->executeSQL($sql);
    }

    /**
     * Initially creates the table Groups when the database is installed.
     * @param <link identifier> $db is the database handle.
     */
    public function createTable() {
        $this->dropTable();
        $sql = "
            CREATE TABLE  `Groups` (
              `ID` int(11) NOT NULL AUTO_INCREMENT,
              `name` varchar(50) NOT NULL DEFAULT '',
              `gRead` tinyint(1) NOT NULL DEFAULT '0',
              `gInsert` tinyint(1) NOT NULL DEFAULT '0',
              `gUpdate` tinyint(1) NOT NULL DEFAULT '0',
              `gCreate` tinyint(1) NOT NULL DEFAULT '0',
              `gDelete` tinyint(1) NOT NULL DEFAULT '0',
              PRIMARY KEY (`ID`),
              UNIQUE KEY `nameK` (`name`)
            ) ENGINE=MyISAM DEFAULT CHARSET=latin1
        ";
        $rs = $this->db->executeSQL($sql);
        if(!$rs)
            return(false);
        return(true);
    }
    public function insertInitialData() {
        $sql = "INSERT INTO Groups (name,gRead,gInsert,gUpdate,gCreate,gDelete)
                VALUES  ('guest',1,0,0,0,0),
                        ('administrator',1,1,1,1,1)";
        $rs = $this->db->executeSQL($sql);
    }

    /**
     *  Getter Methods follow
     *
     */
    public function getID() {
        return($this->id);
    }
}
?>
